home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12346 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help: 2 short functions
  5. Date: 31 Mar 1996 14:06:01 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4jm3g9$b70@sparcserver.lrz-muenchen.de>
  9. References: <4ji734$n6v@masala.cc.uh.edu>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. st7jr@Rosie.UH.EDU (odie garfield) writes:
  13.  
  14. >Hi, I have two functions that I'm not too sure about.
  15. >The first one:
  16.  
  17. >char *get_filename(void)
  18. >{
  19. >   char string[20],*stringp;
  20. >   scanf("%s",string);
  21. >   stringp=&string[0];
  22. >   return stringp;
  23. >}
  24. >I'm want the function to read in a string and return a pointer to that string.
  25. >Am I doing it correctly?
  26.  
  27. No, you're not.
  28.  
  29. 1) scanf is not a good method to get input from anything but a
  30.    well-known source. 
  31. 2) "string" _is_ "&string[0]" in an expression context.
  32. 3) You are returning a pointer to a local variable
  33.  
  34. >My second function is related to the first function:
  35.  
  36. >FILE *open_file(char *filenamep)
  37. >{
  38. >   FILE *fp;
  39. >   fp=fopen("??????","r");
  40. >   return fp;
  41. >}
  42. >In this 2nd function, I want to open a file using the pointer to a string I
  43. >got from the first function. But I don't know exactly how to do this. Any help
  44. >would be appreciated. Thanks.
  45.  
  46. Have you tried "fp = fopen(filenamep, "r");"?
  47.  
  48. Kurt
  49. --
  50. | Kurt Watzka                             Phone : +49-89-2180-6254
  51. | watzka@stat.uni-muenchen.de
  52.